package com.bdqn.jk.controller.admin;


import com.bdqn.jk.common.ServiceResultEnum;
import com.bdqn.jk.entity.Carousel;
import com.bdqn.jk.service.CarouselService;
import com.bdqn.jk.util.PageQueryUtil;
import com.bdqn.jk.util.Result;
import com.bdqn.jk.util.ResultGenerator;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;



import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;

@Controller
@RequestMapping("/admin")
public class NewBeeMallCarouselController {

    @Resource
    CarouselService carouselService;

    //点击跳转页面
    @GetMapping("/carousels")
    public String carouselPage(HttpServletRequest request){
        request.setAttribute("path","newbee_mall_carousel");
        return "newbee_mall_carousel";
    }

    //得到轮播图信息列表
    @RequestMapping(value = "/carousels/list",method = RequestMethod.GET)
    @ResponseBody
    public Result getlist(@RequestParam Map<String,Object> params){
        if (StringUtils.isEmpty(params.get("page"))||StringUtils.isEmpty(params.get("limit"))){
            return ResultGenerator.genFailResult("异常！");
        }
        PageQueryUtil pageQueryUtil =new PageQueryUtil(params);
        return ResultGenerator.genSuccessResult(carouselService.getCarouselPage(pageQueryUtil));
    }

    //添加
    @RequestMapping(value = "/carousels/save",method = RequestMethod.POST)
    @ResponseBody
    public Result save(@RequestBody Carousel carousel){
        if (StringUtils.isEmpty(carousel.getCarouselUrl())|| Objects.isNull(carousel.getCarouselRank())){
            return ResultGenerator.genFailResult("异常！");
        }
        String result = carouselService.saveCarousel(carousel);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)){
            return ResultGenerator.genSuccessResult();
        }else {
            return ResultGenerator.genFailResult(result);
        }
    }

    //修改
    @RequestMapping(value = "/carousels/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(@RequestBody Carousel carousel){
        if (Objects.isNull(carousel.getCarouselId())||StringUtils.isEmpty(carousel.getCarouselUrl())||Objects.isNull(carousel.getCarouselRank())){
            return ResultGenerator.genFailResult("异常！");
        }
        String result = carouselService.updateCarousel(carousel);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)){
            return ResultGenerator.genSuccessResult();
        }else {
            return ResultGenerator.genFailResult(result);
        }
    }

    //删除
    @RequestMapping(value = "/carousels/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@RequestBody Integer[] integers){
        if (integers.length<1){
            return ResultGenerator.genFailResult("异常！");
        }
        if (carouselService.deleteBatch(integers)){
            return ResultGenerator.genSuccessResult();
        }else {
            return ResultGenerator.genFailResult("删除失败！");
        }
    }

    //详细信息
    @RequestMapping(value = "/carousels/info{id}")
    @ResponseBody
    public Result getinfo(@PathVariable("id")Integer id){
        Carousel carousel=carouselService.getCarouselById(id);
        if (carousel ==null){
            return ResultGenerator.genFailResult(ServiceResultEnum.DATA_NOT_EXIST.getResult());
        }
        return ResultGenerator.genSuccessResult(carousel);
    }
}
